﻿using NewsMine.Storage.SQLite;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.IO;
using System.Collections.Generic;

namespace NewsMine.Storage.Tests
{
    
    
    /// <summary>
    ///This is a test class for SQLiteFeedStoreHelperTest and is intended
    ///to contain all SQLiteFeedStoreHelperTest Unit Tests
    ///</summary>
    [TestClass()]
    public class SQLiteFeedStoreHelperTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        /// <summary>
        ///A test for SQLiteFeedStoreHelper Constructor
        ///</summary>
        [TestMethod()]
        public void SQLiteFeedStoreHelperConstructorTest()
        {
            string sqliteFilePath = "c:\\testSqliteStore.db";
            SQLiteFeedStoreHelper target = new SQLiteFeedStoreHelper(sqliteFilePath);

            File.Delete(sqliteFilePath);
        }

        [TestMethod()]
        public void AddFeedItemTest()
        {
            string sqliteFilePath = "c:\\testSqliteStore.db";

            SQLiteFeedStoreHelper target = new SQLiteFeedStoreHelper(sqliteFilePath);

            List<long> insertedIds = new List<long>();

            var feed = target.AddFeedToStore(GetTestFeedItemObject());

            Assert.IsTrue(!string.IsNullOrEmpty(feed.ID));

            insertedIds.Add(Convert.ToInt64(feed.ID));

            feed = target.AddFeedToStore(GetTestFeedItemObject());

            Assert.IsTrue(!string.IsNullOrEmpty(feed.ID));

            insertedIds.Add(Convert.ToInt64(feed.ID));
            feed = target.AddFeedToStore(GetTestFeedItemObject());

            Assert.IsTrue(!string.IsNullOrEmpty(feed.ID));

            insertedIds.Add(Convert.ToInt64(feed.ID));
            feed = target.AddFeedToStore(GetTestFeedItemObject());

            Assert.IsTrue(!string.IsNullOrEmpty(feed.ID));

            insertedIds.Add(Convert.ToInt64(feed.ID));
            feed = target.AddFeedToStore(GetTestFeedItemObject());

            Assert.IsTrue(!string.IsNullOrEmpty(feed.ID));

            insertedIds.Add(Convert.ToInt64(feed.ID));
            feed = target.AddFeedToStore(GetTestFeedItemObject());

            Assert.IsTrue(!string.IsNullOrEmpty(feed.ID));

            insertedIds.Add(Convert.ToInt64(feed.ID));
            feed = target.AddFeedToStore(GetTestFeedItemObject());

            Assert.IsTrue(!string.IsNullOrEmpty(feed.ID));

            insertedIds.Add(Convert.ToInt64(feed.ID));
            feed = target.AddFeedToStore(GetTestFeedItemObject());

            Assert.IsTrue(!string.IsNullOrEmpty(feed.ID));

            insertedIds.Add(Convert.ToInt64(feed.ID));

            var gotFeed = target.GetFeedItemsByIDs(insertedIds.ToArray());

            Assert.IsTrue(gotFeed != null);
            Assert.IsTrue(gotFeed.Count>0);

            foreach (var f in gotFeed)
            {
                Assert.IsTrue(!string.IsNullOrEmpty(f.ID));
            }

            //File.Delete(sqliteFilePath);
        }

        private static DomainObjects.FeedItem GetTestFeedItemObject()
        {
            return new DomainObjects.FeedItem
            {
                Author = "my feed author",
                Body = "my feed body",
                category = "my feed category",
                Description = "my feed description",
                Domain = "my feed domain",
                ImageUrl = "my feed image url",
                Keyworkds = "my keywords",
                Language = "my language",
                Link = "My link",
                PubDate = DateTime.Now,
                Rank = 233,
                RefUrl = "my ref url",
                Tags = new System.Collections.Generic.List<string> { "tag1", "tag2" },
                ThumbImageUrl = "my thumb url",
                Title = "my title",
                LastTouchTime = DateTime.Now,
                UpdatedDateTime = DateTime.Now

            };
        }
    }
}
